using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SlotSelector : EventCommander
{
	[SerializeField]
	private bool showSectors = true;

	[SerializeField]
	private bool activated;

	[SerializeField]
	private float disableTime;

	[SerializeField]
	private float delayTime;

	[SerializeField]
	private int slotCount;

	public UILabel label;

	[SerializeField]
	private UILabel alternativeLabel;

	public UITexture sellectorTexture;

	[SerializeField]
	private List<SlotSelectorSector> slotSectors = new List<SlotSelectorSector>();

	[SerializeField]
	private Transform parentPanel;

	[SerializeField]
	private GameObject colliderOnOpen;

	public bool blocked;

	[SerializeField]
	private bool isOnButtonCallCar;

	[SerializeField]
	private UITexture carIcon;

	private Animator _animator;

	private SlotSelectorSector selectedSlot;

	private UIPanel panel;

	private UIPanel lastObj;

	public bool forceBlock;

	private bool lastViseble = true;

	private bool needRefresh = true;

	private BoxCollider mainCollider;

	private IEnumerator slotDisabler;

	private IEnumerator slotDisablerDelay;

	private bool noAmmoShown;

	public Animator animator
	{
		get
		{
			if (_animator == null)
			{
				_animator = GetComponent<Animator>();
			}
			return _animator;
		}
	}

	public string text
	{
		get
		{
			if (label != null)
			{
				return label.text;
			}
			return string.Empty;
		}
		set
		{
			if (label != null)
			{
				label.text = value;
			}
			if (alternativeLabel != null)
			{
				alternativeLabel.text = value;
			}
		}
	}

	public override void Init()
	{
		base.Init();
		animator.logWarnings = false;
		InitEvents(new string[1] { "SlotSelectorSlotSellected" });
	}

	public override void Start()
	{
		base.Start();
		if (!TutorialManager.TuturialWorking())
		{
			lastObj = GetComponentInParent<UIPanel>();
			if (parentPanel != null)
			{
				panel = GetComponentInParent<UIPanel>();
				base.transform.parent = parentPanel;
			}
			else
			{
				panel = base.gameObject.AddComponent<UIPanel>();
				panel.depth = GetComponent<UIWidget>().depth;
				base.transform.parent = GameUI.instance.transform.parent;
				panel.UpdateAnchors();
				panel.gameObject.SetActive(false);
				panel.gameObject.SetActive(true);
			}
		}
		if (GetComponent<Collider>() == null)
		{
			base.gameObject.AddComponent<BoxCollider>();
		}
		int i;
		for (i = 0; i < slotSectors.Count; i++)
		{
			slotSectors[i].AddEventListener(this, "SlotSellected", delegate(Object sender, object[] args)
			{
				SelectSlot((SlotSelectorSector)sender);
			});
			slotSectors[i].AddEventListener(this, "ActiveSlotTextChanged", delegate(Object sender, object[] args)
			{
				text = (string)args[0];
			});
			slotSectors[i].AddEventListener(this, "SlotVisiableChange", delegate(Object sender, object[] args)
			{
				blocked = !(bool)args[0];
				if (blocked || slotSectors.FindAll((SlotSelectorSector x) => x.slot != null && x.slot.elementName != string.Empty).Count < 2)
				{
					ActivateAllSlots(false);
				}
			});
			slotSectors[i].AddEventListener(this, "StartInterfaceAnimation", delegate(Object sender, object[] args)
			{
				animator.gameObject.SetActive((bool)args[1]);
				animator.SetFloat("SpeedMultipler", 1f / (float)args[0]);
				animator.ResetTrigger("Reload");
				animator.SetTrigger("Reload");
			});
			slotSectors[i].AddEventListener(this, "StopInterfaceAnimation", delegate
			{
				animator.SetTrigger("Reset");
			});
			slotSectors[i].AddEventListener(this, "AlternativeText", delegate(Object sender, object[] args)
			{
				if (!(bool)args[0])
				{
					ToggleNoAmmo(false);
				}
				else
				{
					ShowNoAmmooForTime();
					if ((bool)alternativeLabel)
					{
						alternativeLabel.text = (string)args[1];
					}
				}
			});
			slotSectors[i].AddEventListener(this, "SlotRemoved", delegate
			{
				string mostExpensiveEquipedWeaponSlot = MarketController.Instance.GetMostExpensiveEquipedWeaponSlot();
				int num = 0;
				while (i < slotSectors.Count)
				{
					if (slotSectors[num].slotName == mostExpensiveEquipedWeaponSlot)
					{
						SelectSlot(slotSectors[num]);
						break;
					}
					num++;
				}
			});
			if (slotSectors[i].selected)
			{
				SelectSlot(slotSectors[i]);
			}
			slotSectors[i].Active(false);
		}
	}

	private void Update()
	{
		if ((bool)lastObj && GameUI.instance != null)
		{
			SetVisible(lastObj.gameObject.activeSelf && GameUI.instance.gameObject.activeSelf && !forceBlock);
		}
		if (!activated || blocked || slotDisablerDelay == null)
		{
			return;
		}
		Touch[] touches = Input.touches;
		for (int i = 0; i < touches.Length; i++)
		{
			Touch touch = touches[i];
			if (!(touch.position.x > (float)(Screen.width / 2)) || touch.phase != 0)
			{
				continue;
			}
			for (int j = 0; j < slotSectors.Count; j++)
			{
				if (InRange(slotSectors[j].slot.Texture, touch.position))
				{
					StopCoroutine(slotDisablerDelay);
					return;
				}
			}
			OnPress(false);
		}
		if (!Input.GetMouseButtonDown(0) || !(Input.mousePosition.x > (float)(Screen.width / 2)))
		{
			return;
		}
		for (int k = 0; k < slotSectors.Count; k++)
		{
			if (slotSectors[k].slot != null && (bool)slotSectors[k].slot.Texture && InRange(slotSectors[k].slot.Texture, Input.mousePosition))
			{
				StopCoroutine(slotDisablerDelay);
				return;
			}
		}
		OnPress(false);
	}

	public void OnPress(bool isDown)
	{
		if ((PlayerBehavior.MyPlayer != null && PlayerBehavior.MyPlayer.inCar) || blocked)
		{
			return;
		}
		if (isDown)
		{
			if (!activated)
			{
				SetActive(true, false);
			}
			animator.ResetTrigger("Klick");
			animator.SetTrigger("Klick");
		}
		else if (activated)
		{
			SetActive(false, slotDisablerDelay == null);
		}
	}

	public void SetVisible(bool val)
	{
		if (lastViseble == val)
		{
			return;
		}
		ButtonPositionLoader component = GetComponent<ButtonPositionLoader>();
		if ((bool)component && val)
		{
			component.OnEnable();
		}
		UIBasicSprite[] componentsInChildren = GetComponentsInChildren<UIBasicSprite>(true);
		foreach (UIBasicSprite uIBasicSprite in componentsInChildren)
		{
			uIBasicSprite.enabled = val && !forceBlock;
			uIBasicSprite.UpdateAnchors();
		}
		if (val && isOnButtonCallCar && carIcon.mainTexture == null && carIcon.enabled && PrivateCarManager.instance != null && PrivateCarManager.instance.currentCar != null)
		{
			Texture texture = Resources.Load<Texture>("AtlasGame/" + PrivateCarManager.instance.currentCar.nameSpritePreview);
			if (texture == null)
			{
				texture = Resources.Load<Texture>("AtlasGame/AG_" + PrivateCarManager.instance.currentCar.GetCurrentSkin().name);
			}
			carIcon.mainTexture = texture;
		}
		lastViseble = val;
		if ((bool)panel && needRefresh)
		{
			panel.Refresh();
			needRefresh = false;
		}
		UILabel[] componentsInChildren2 = GetComponentsInChildren<UILabel>(true);
		foreach (UILabel uILabel in componentsInChildren2)
		{
			uILabel.enabled = val && !forceBlock;
			uILabel.UpdateAnchors();
		}
		if (mainCollider == null)
		{
			mainCollider = GetComponent<BoxCollider>();
		}
		mainCollider.enabled = val;
	}

	public void SelectSlot(SlotSelectorSector sector)
	{
		if (sector != null)
		{
			for (int i = 0; i < slotSectors.Count; i++)
			{
				if (slotSectors[i] != sector)
				{
					slotSectors[i].UnSelect();
				}
			}
			RiseEvent(this, "SlotSelectorSlotSellected", sector.slot);
			if (sellectorTexture != null && sector.slot.Texture != null)
			{
				sellectorTexture.mainTexture = sector.slot.Texture.mainTexture;
				text = sector.text;
				if (sector != selectedSlot)
				{
					animator.SetTrigger("BreackAnimation");
					selectedSlot = sector;
				}
			}
		}
		SetActive(false, false);
	}

	public bool InRange(UIBasicSprite sprite, Vector2 pos)
	{
		Vector3[] localCorners = sprite.localCorners;
		Vector3 vector = UICamera.currentCamera.WorldToScreenPoint(sprite.transform.position);
		vector = new Vector3(vector.x - (float)(Screen.width / 2), vector.y - (float)(Screen.height / 2), 0f);
		pos = new Vector2(pos.x - (float)(Screen.width / 2), pos.y - (float)(Screen.height / 2));
		if (pos.x > localCorners[0].x + vector.x && pos.y > localCorners[0].y + vector.y && pos.x > localCorners[1].x + vector.x && pos.y < localCorners[1].y + vector.y && pos.x < localCorners[2].x + vector.x && pos.y < localCorners[2].y + vector.y && pos.x < localCorners[3].x + vector.x && pos.y > localCorners[3].y + vector.y)
		{
			return true;
		}
		return false;
	}

	public void SetSlotNames(string[] names)
	{
		for (int i = 0; i < slotSectors.Count; i++)
		{
			if (i < names.Length && slotSectors[i].slotName == string.Empty)
			{
				slotSectors[i].slotName = names[i];
			}
		}
	}

	public void SetSlot(string slotName, ISlot slot)
	{
		SlotSelectorSector slotSelectorSector = slotSectors.Find((SlotSelectorSector x) => x.slotName == slotName);
		if ((bool)slotSelectorSector)
		{
			slotSelectorSector.slot = slot;
		}
	}

	public void RemoveSlot(string slotName)
	{
		if ((bool)slotSectors.Find((SlotSelectorSector x) => x.slot.slotName == slotName))
		{
			SlotSelectorSector slotSelectorSector = slotSectors.Find((SlotSelectorSector x) => x.slotName == slotName);
			slotSectors.Remove(slotSelectorSector);
			Object.Destroy(slotSelectorSector);
			slotCount--;
		}
	}

	public void ActivateSlot(int slotNum, bool val)
	{
		slotSectors[slotNum].Active(val);
	}

	public void ActivateSlot(string slotName, bool val)
	{
		ActivateSlot(slotSectors.IndexOf(slotSectors.Find((SlotSelectorSector x) => x.slotName == slotName)), val);
	}

	public void ActivateSlot(ISlot slot, bool val)
	{
		ActivateSlot(slotSectors.IndexOf(slotSectors.Find((SlotSelectorSector x) => x.slot == slot)), val);
	}

	public void SetActive(bool val, bool useDelay)
	{
		if (GameController.instance.curTypeGame == TypeGame.Zombe && slotSectors.FindAll((SlotSelectorSector x) => x.slot != null && x.slot.elementName != string.Empty).Count < 2)
		{
			return;
		}
		if (slotDisablerDelay != null && slotDisabler != null)
		{
			StopCoroutine(slotDisabler);
			slotDisabler = null;
		}
		if (useDelay)
		{
			slotDisablerDelay = ActivateAllSlotsWithDelay(val);
			StartCoroutine(slotDisablerDelay);
			return;
		}
		if (val && slotDisablerDelay != null)
		{
			StopCoroutine(slotDisablerDelay);
			slotDisablerDelay = null;
		}
		if (base.gameObject.activeSelf)
		{
			slotDisabler = ActivateAllSlots(val);
			StartCoroutine(slotDisabler);
		}
		else
		{
			ActiveAll(val);
		}
	}

	private void ActiveAll(bool val)
	{
		for (int i = 0; i < slotSectors.Count; i++)
		{
			slotSectors[i].Select(val);
		}
	}

	private IEnumerator ActivateAllSlotsWithDelay(bool val)
	{
		yield return new WaitForSeconds(disableTime);
		slotDisablerDelay = null;
		slotDisabler = ActivateAllSlots(val);
		StartCoroutine(slotDisabler);
	}

	private IEnumerator ActivateAllSlots(bool val)
	{
		activated = val;
		for (int i = 0; i < slotSectors.Count; i++)
		{
			if (showSectors)
			{
				ActivateSlot(i, val);
				yield return new WaitForSeconds(delayTime);
			}
			else if (val)
			{
				slotSectors[i].Select(true);
			}
		}
		yield return null;
		slotDisabler = null;
	}

	public override void OnDisable()
	{
		for (int i = 0; i < slotSectors.Count; i++)
		{
			ActivateSlot(i, false);
		}
		activated = false;
	}

	private void ToggleNoAmmo(bool value)
	{
		alternativeLabel.gameObject.SetActive(value);
		noAmmoShown = value;
		AimHUD.instance.ToggleAimHUD(!value);
	}

	private void ShowNoAmmooForTime()
	{
		if (!noAmmoShown)
		{
			StartCoroutine(ShowNoAmmoForTimeCoroutine());
		}
	}

	private IEnumerator ShowNoAmmoForTimeCoroutine()
	{
		ToggleNoAmmo(true);
		yield return new WaitForSeconds(1.5f);
		while (GameController.instance.playerScript.isShooting)
		{
			yield return null;
		}
		ToggleNoAmmo(false);
	}
}
